package com.beauty.dressing.module.mine.persenter;

import com.beauty.dressing.common.base.BasePresenter;
import com.beauty.dressing.common.bean.BaseObserver;
import com.beauty.dressing.common.bean.BaseParse;
import com.beauty.dressing.common.bean.UserInfoBean;
import com.beauty.dressing.module.mine.persenter.contract.UserInfoContract;
import com.beauty.dressing.rxandroid.api.ApiManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserInfoPresenter extends BasePresenter implements UserInfoContract.Presenter {
    private static final String TAG = "UserInfoPresenter";
    private UserInfoContract.ViewControl mViewControl;

    public UserInfoPresenter(UserInfoContract.ViewControl mViewControl) {
        this.mViewControl = mViewControl;
    }


    @Override
    public void editName(String member_id, String membername, String token) {
        mViewControl.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("member_id", member_id);
        map.put("membername", membername);
        map.put("token", token);
        ApiManager.getInstence().getMemberServie().editName(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, Object>() {
                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onEditName(true, 200, data.toString(), list);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onEditName(false, code, message, list);
                    }

                });
    }

    @Override
    public void editImage(String account, String image, String type, String token) {
        mViewControl.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("account", account);
        map.put("image", image);
        map.put("type", type);
        map.put("token", token);
        ApiManager.getInstence().getMemberServie().edit_image(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object, Object>() {

                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onEditImage(true, 200, message);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onEditImage(false, code, message);
                    }

                });

    }

    @Override
    public void memberInfo(String token, long member_id) {
        mViewControl.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("member_id", String.valueOf(member_id));
        map.put("token", token);
        ApiManager.getInstence().getMemberServie().memberInfo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UserInfoBean, Object>() {


                    @Override
                    protected void onSuccees(String message, UserInfoBean data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onMemberInfo(true, 200, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, UserInfoBean data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onMemberInfo(false, code, data);
                    }
                });

    }
}
